package com.gexiaobao.pigeon.ui.home

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gexiaobao.pigeon.app.model.bean.MemberCodeResponse
import com.gexiaobao.pigeon.app.model.bean.MessageListResponse
import com.gexiaobao.pigeon.app.model.bean.NoticeInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.LoftPayInfoParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

class HomeViewModel : BaseViewModel() {

    private var pageNo: Int = 1

    val myToolsMore = BooleanObservableField()
    val userName = StringObservableField()

    /**  消息列表  */
    var messageList: MutableLiveData<ListDataUiState<MessageListResponse.DataList>> = MutableLiveData()

    /**  获取单条消息  */
    var singleMsgResult: MutableLiveData<NoticeInfoResponse> = MutableLiveData()

    var showMyTools = object : ObservableInt(myToolsMore) {
        override fun get(): Int {
            return if (myToolsMore.get()) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }
    }

    //获取棚号
    var memberNumResult: MutableLiveData<MemberCodeResponse> = MutableLiveData()
    fun getMemberNum(id: String) {
        request({ apiService.getMemberNum(id) }, {
            memberNumResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    fun getMessageList(isRefresh: Boolean, messageType: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMessageList(pageNo, 20, "2", "", messageType, "") }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                hasMore = it.hasMore(),
                listData = it.data
            )
            messageList.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(isSuccess = false, isRefresh = isRefresh, errMessage = it.msg, listData = arrayListOf<MessageListResponse.DataList>())
            messageList.value = listDataUiState
        })
    }

    fun getMessageResult(id: String) {
        request({ apiService.getMessageResult(id) }, {
            singleMsgResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 删除消息 */
    val deleteMsgResult: MutableLiveData<UiState> = MutableLiveData()
    fun deleteMessage(id: String) {
        request({ apiService.deleteMessage(id) }, {
            //请求成功
            val result = UiState(isSuccess = true)
            deleteMsgResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            deleteMsgResult.value = result
        })
    }

}